/**
 * Created by apple on 2017/7/29.
 */
var express = require('express');
var router = express.Router();

var UserMoneyRouter = module.exports;
var UserToken = require('../db/user_tokens.js');
var UserMoneyDao = require('../dao/user_money');




/**
 * 结算跑单补贴
 */
UserMoneyRouter.balanceSubsidy = function (req, res) {
    var par = paramAll(req);
    if(!checkSrvConnection(par)) return res.json(new ERR('访问受限',400));
    console.log(par);
    if(!par.waitBalanceInfos) return res.json(new ERR('访问受限',400));
    try {
        par.waitBalanceInfos = JSON.parse(par.waitBalanceInfos);
    }
    catch (e) {
        res.json(new ERR(e, 400));
        console.log(e);
        return;
    }
    UserMoneyDao.balanceSubsidy(par, function (err, result) {
        if (err) {
            res.json(new ERR(err, 400));
            return;
        }
        res.json(new PKG(result));
    });
}
router.post('/balanceSubsidy', UserMoneyRouter.balanceSubsidy);


/**
 * 结算u省u赚佣金
 */
UserMoneyRouter.balanceCommission = function (req, res) {
    var par = paramAll(req);
    if(!checkSrvConnection(par)) return res.json(new ERR('访问受限',400));
    if(!par.waitBalanceInfos) return res.json(new ERR('访问受限',400));
    try {
        par.waitBalanceArr = JSON.parse(par.waitBalanceInfos);
    }
    catch (e) {
        res.json(new ERR(e, 400));
        console.log(e);
        return;
    }
    UserMoneyDao.balanceCommission(par, function (err, result) {
        if (err) {
            res.json(new ERR(err, 400));
            return;
        }
        res.json(new PKG(result));
    });
}
router.post('/balanceCommission', UserMoneyRouter.balanceCommission);


UserMoneyRouter.getMoneyList = function (req,res) {
    var par = paramAll(req);
    UserToken.checkLogin(par.token,function (err,userInfo) {
        if(err) return res.json(new ERR(err, 400));
        if(!userInfo) return res.json(new ERR('请先登录', 403));

        par.uid = userInfo.uid;
        if (!par.app_sid) par.app_sid = 'usuz';
        UserMoneyDao.getMoneyList(par,function (err,result) {
            if(err) return res.json(new ERR(err, 400));
            res.json(new PKG(result));
        })
    });
}
router.post('/getMoneyList', UserMoneyRouter.getMoneyList);

/**
 *
 */
UserMoneyRouter.getRatioByAccounts = function (req, res) {
    var par = paramAll(req);
    if(!checkSrvConnection(par)) return res.json(new ERR('访问受限',400));
    if(!par.appSid) return res.json(new ERR('appSid不能为空！',400));
    if(!par.type) return res.json(new ERR('type不能为空！',400));
    if(!par.accounts) return res.json(new ERR('请用 accounts 传输账号数组！',400));
    try {
        par.accounts = JSON.parse(par.accounts);
    }
    catch (e) {
        res.json(new ERR(e, 400));
        console.log(e);
        return;
    }
    UserMoneyDao.getRatioByAccounts(par.appSid, par.accounts,par.type, function (err, result) {
        if (err) {
            res.json(new ERR(err, 400));
            return;
        }
        res.json(new PKG(result));
    });
}
router.post('/getRatioByAccounts', UserMoneyRouter.getRatioByAccounts);


/**
 * 流水记录
 * @param  token
 * @param  account
 * @param  start_time
 * @param  end_time
 * @param  app_sid
 *         url//userMoney/record
 */
UserMoneyRouter.record = function (req, res) {
    var par = paramAll(req);
    par.app_sid = !par.app_sid ? 'usuz' : par.app_sid;
    // if(!par.account) return res.json(new ERR('account不能为空！',400));
    UserToken.checkAdminAllLogin(par.token, par.app_sid, 'qudao', function(err, errCode, adminInfo) {
        if(err) return res.json(new ERR(err, errCode));
        UserMoneyDao.record(par, function (err, result) {
            if (err)  return res.json(new ERR(err, 400));
            res.json(new PKG(result));
        });
    })
}
router.post('/record', UserMoneyRouter.record);

module.exports = router;